<template>
  <!-- 新增编辑的弹层 -->
  <el-dialog :visible="showDialog" title="弹层标题" @close="showDialogg ">
    <!-- 表单内容 -->
    <!-- 表单内容 -->
    <el-form ref="formData" label-width="100px" :model="formData" :rules="rules">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="formData.description" />
      </el-form-item>
      <el-form-item label="权限启用">
        <el-switch
          v-model="formData.enVisible"
          active-text="启用"
          active-value="1"
          inactive-text="不启用"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: right;">
        <el-button @click="showDialogg">取消</el-button>
        <el-button type="primary" @click="submitclick">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { AddpermisionAPI } from '@/api/permission'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    showDialogg() {
      this.$emit('update:showDialog', false)
      this.$refs.formData.resetFields()
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    /**
     * 接收参数，区分添加位置
     */
    opendata(type, pid) {
      console.log(type, pid)
      this.formData.type = type
      this.formData.pid = pid
    },
    /**
     * 兜底校验，添加权限点
     */
    submitclick() {
      this.$refs.formData.validate(async(flag) => {
        if (!flag) return
        await AddpermisionAPI(this.formData)
        this.$message.success('添加成功')
        this.showDialogg()
        this.$parent.permissionList()
      })
    }
  }
}

</script>

<style>

</style>
